package com.fzy.servlet;

import com.fzy.Dao.EmployeeDao;
import com.fzy.Dao.EmployeeDaoImpl;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/DeleteEmployeeServlet")
public class DeleteEmployeeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String employeeid = request.getParameter("employeeid");
        System.out.println(employeeid);

        String message;

        try {
            EmployeeDao employeeDao = new EmployeeDaoImpl();
            int rs = employeeDao.DeleteEmployee(employeeid);
            System.out.println("re");
            if (rs > 0) {
                message = "删除成功";
                System.out.println("成功");
            } else {
                message = "删除失败，员工ID不存在";
                System.out.println("不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            message = "删除出错: " + e.getMessage();
        }
        System.out.println("out");
        // 使用session存储消息，重定向后仍可访问
        request.getSession().setAttribute("message", message);
        response.sendRedirect("GetAllEmployeeServlet");
    }
}
